package com.example.musicplayer.adapter;


import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.graphics.drawable.RoundedBitmapDrawable;
import androidx.core.graphics.drawable.RoundedBitmapDrawableFactory;

import com.example.musicplayer.R;
import com.example.musicplayer.activity.MusicActivity;
import com.example.musicplayer.bean.Album;
import com.example.musicplayer.fragment.MainTabMyFragment;
import com.example.musicplayer.utils.MusicUtils;

import java.util.List;

public class MainTabMyAdapter extends BaseAdapter {
    private Context context;
    private List<Album> list;
    public MainTabMyAdapter(MainTabMyFragment mainTabMyFragment, List<Album> list) {
        this.context = mainTabMyFragment.getContext();
        this.list = list;

    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            //引入布局
            view = View.inflate(context, R.layout.item_main_tab_listview, null);
            //实例化对象
            holder.imageView = (ImageView) view.findViewById(R.id.item_iv_cover);
            holder.name = (TextView) view.findViewById(R.id.item_main_tab_listname);
            holder.number = (TextView) view.findViewById(R.id.item_main_tab_number);
            view.setTag(holder);

        } else {
            holder = (ViewHolder) view.getTag();
        }
        //给控件赋值
        holder.imageView.setImageBitmap(list.get(i).getAlbumCover());
        holder.name.setText(list.get(i).getName().toString());
        holder.number.setText(list.get(i).getNumber()+"首");

        Bitmap finalBitmap = (list.get(i).getAlbumCover());
        RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory.create(context.getResources(), finalBitmap); //创建RoundedBitmapDrawable对象
        roundedBitmapDrawable.setCornerRadius(100); //设置圆角Radius（根据实际需求）
        roundedBitmapDrawable.setAntiAlias(true); //设置抗锯齿
        holder.imageView.setImageDrawable(roundedBitmapDrawable);

        return view;
    }
    class ViewHolder{
        ImageView imageView;//封面
        TextView name;//歌单名
        TextView number;//歌曲数量
    }

}